package com.tansun.easycare.rule.common.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tansun.easycare.rule.common.service.RuleRefCommService;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.executionblock.entity.RuleExcutionBlock;
import com.tansun.easycare.rule.executionblock.service.ExecutionBlockService;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.referencedata.service.RuleReferenceDataService;
import com.tansun.easycare.rule.ruleflow.entity.RuleFlowRects;
import com.tansun.easycare.rule.ruleflow.service.RuleFlowRectsService;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruleversion.service.RuleVersioninfoService;
import com.tansun.rule.common.RuleModelTypeEnum;
import com.tansun.rule.utils.StringUtils;

@Service
public class RuleRefCommServiceImpl implements RuleRefCommService {
	
	@Autowired
	private  RuleManageService  ruleManageService;
	
	@Autowired
	private  ExecutionBlockService  executionBlockService;
	
	@Autowired
	private  RuleReferenceDataService  ruleReferenceDataService;
	@Autowired
	private  RuleFlowRectsService  ruleFlowRectsService;
	
	@Autowired
	private  RuleVersioninfoService  ruleVersioninfoService;

	@Override
	public List<RuleReferenceData> findExcBlockRefData(String projectCode, String code, String version)
			throws BizException {
    
		RuleManage  ruleManageInfo  = new  RuleManage();
		ruleManageInfo.setCode(code);
		ruleManageInfo.setProjectCode(projectCode);
		ruleManageInfo.setVersion(version);
		List<RuleManage> list =this.ruleManageService.findList(ruleManageInfo);
		List<RuleReferenceData>  results  = new  ArrayList<>();
		if (list != null && list.size()>0){
			RuleExcutionBlock  excutionBlock = new  RuleExcutionBlock();
			excutionBlock.setManageId(list.get(0).getId());
			excutionBlock.setBlockVersion(list.get(0).getVersion());
			List<RuleExcutionBlock> eList =executionBlockService.findList(excutionBlock);
			RuleReferenceData referenceData  = new  RuleReferenceData();
			referenceData.setProjectCode(projectCode);
			referenceData.setCode(code);
			referenceData.setVersion(version);
			results.addAll(this.ruleReferenceDataService.findReferenceDataList(referenceData));
			for (RuleExcutionBlock ruleExcutionBlock :eList){
				if (RuleModelTypeEnum.DecisionTree.code.equals(ruleExcutionBlock.getModelType())){
					//this.findRuleFlowReferenceData(projectCode, code, version, results);
	            	Map<String,String> vMap  = new  HashMap<String,String>();
	            	vMap.put("projectCode", projectCode);
	            	vMap.put("code", ruleExcutionBlock.getExecCode());
	            	List<String>  strList = this.ruleVersioninfoService.findRuleRefeCodes(vMap);
	            	if (strList !=null && strList.size()>0) {
		            	Map<String,Object>  oMap  = new  HashMap<>();
		            	oMap.put("projectCode", projectCode);
		            	oMap.put("refeList", strList);
	            		results.addAll(this.ruleReferenceDataService.findModelReferenceList(oMap));
	            	}
				}
				referenceData  = new  RuleReferenceData();
				referenceData.setProjectCode(projectCode);
				referenceData.setCode(ruleExcutionBlock.getExecCode());
				RuleManage rm = ruleManageService.findByCode(ruleExcutionBlock.getExecCode(), projectCode);
				if(rm != null) {
					referenceData.setVersion(rm.getActiveVersion());
					results.addAll(this.ruleReferenceDataService.findReferenceDataList(referenceData));
				}
			}
		}
		return results;
	}
	
	public   List<RuleReferenceData>   findRuleFlowReferenceData(String projectCode,String  code,String version,List<RuleReferenceData> refList)  throws BizException{
		return findRuleFlowReferenceData(projectCode,code,version,refList,null);
	}
	public   List<RuleReferenceData>   findRuleFlowReferenceData(String projectCode,String  code,String version,List<RuleReferenceData> refList,String contentType)  throws BizException{
		List<RuleReferenceData>  results  = null;
		if (refList == null){
		   results = new  ArrayList();
		}else {
			results  = refList;
		}
		RuleManage  ruleInfo   = new   RuleManage();
		ruleInfo.setCode(code);
		ruleInfo.setVersion(version);
		ruleInfo.setProjectCode(projectCode);
		List<RuleManage> list =this.ruleManageService.findList(ruleInfo);
		if (list == null || list.size()==0){
			throw  new  BizException("E-000600");
		}
		RuleManage  ruleManage  = list.get(0);
		RuleFlowRects  flowRectsInfo  = new  RuleFlowRects();
		flowRectsInfo.setManageId(ruleManage.getId());
		flowRectsInfo.setVersion(version);
		List<RuleFlowRects> rects = this.ruleFlowRectsService.findList(flowRectsInfo);
		Collection<RuleReferenceData> championList = ruleReferenceDataService.findReferenceChampionList(flowRectsInfo);
		results.addAll(championList);
		List<String> listFlag = new ArrayList<String>();
		for (RuleFlowRects  ruleFlowRects :rects){
			 if (!StringUtils.isEmpty(ruleFlowRects.getRectCode())){
				 String rectCode = ruleFlowRects.getRectCode();
				 String rectVersion = ruleFlowRects.getRectVersion();
				 String rectType = ruleFlowRects.getRectType();
				 if(rectType.equals("func")) {
					 RuleReferenceData  referenceData  = new  RuleReferenceData();
					 referenceData.setCode(ruleManage.getCode());
					 referenceData.setNameen(rectVersion);
					 referenceData.setProjectCode(projectCode);
					 referenceData.setVersion(ruleManage.getVersion());
					 results.addAll(this.ruleReferenceDataService.findReferenceDataList(referenceData));
					 this.ruleReferenceDataService.findFuncReferenceDataList(projectCode,rectCode,rectVersion, (Collection<RuleReferenceData>)results);
				 }else {
					 RuleManage rm = ruleManageService.findByCode(rectCode, projectCode);
					 if(rm!=null) {
						 rectVersion = rm.getActiveVersion();
						 rectType = rm.getModelType();
					 }
					 if (RuleModelTypeEnum.ExecutionBlock.code.equals(rectType)){
						 results.addAll( this.findExcBlockRefData(projectCode, rectCode, rectVersion));
					 }else if (RuleModelTypeEnum.RuleFlow.code.equals(rectType)){
						 this.findRuleFlowReferenceData(projectCode, rectCode, rectVersion, results,contentType);
					 }else {
						 this.ruleReferenceDataService.findReferenceDataList(projectCode, rectCode, rectType, rectVersion, contentType,results, listFlag);
						 RuleManage rmm = new RuleManage();
						 rmm.setCode(rectCode);
						 rmm.setVersion(rectVersion);
						 rmm.setProjectCode(projectCode);
						 List<RuleManage> rmlist =this.ruleManageService.findList(rmm);
						 if (rmlist == null || rmlist.size()==0){
						 	throw  new  BizException("E-000600");
						 }
						 ruleInfo = rmlist.get(0);
				         ruleReferenceDataService.findFuncReferenceDataList(rmm, results);
					 }
				 }
			 }	 
		}
		// 获取分支条件变量
        RuleReferenceData ruleReferenceData = new RuleReferenceData();
        ruleReferenceData.setManageId(ruleManage.getId());
        ruleReferenceData.setVersion(version);
        Collection<RuleReferenceData> referenceList = ruleReferenceDataService.findReferenceDataList(ruleReferenceData);
        results.addAll(referenceList);
        Set<RuleReferenceData> setresult = new HashSet<RuleReferenceData>(results);
		return  new ArrayList<>(setresult);
	}	
	
	

}
